import javax.swing.plaf.metal.MetalCheckBoxIcon;

public class Main {
    public static void main(String[] args) {
        Animal animal=new Dog("旺财",2);
        Dog dog=(Dog)animal;
        dog.bark();

        //报错，animal没用引用Bird实例
        //Bird bird=(Bird) animal;
        //bird.fly();

        //instanceof 判断前者是否引用后者
        if(animal instanceof Bird){
            Bird bird=(Bird) animal;
            bird.fly();
        }else {
            System.out.println("animal没用引用Bird实例");
        }

    }
}

class Animal {
    String name;
    int age;

    public Animal() {
    }

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void eat(){
        System.out.println(this.name + " 正在吃...");
    }
}

class Dog extends Animal{
    public Dog(){
    }

    public Dog(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat(){
        System.out.println(this.name + " 正在吃狗粮..");
    }

    public void bark(){
        System.out.println(this.name + " 正在叫");
    }
}

class Bird extends Animal{
    public Bird() {
    }

    public Bird(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat(){
        System.out.println(this.name + " 正在吃鸟粮...");
    }

    public void fly(){
        System.out.println(this.name + " 正在飞...");
    }
}